#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        int n = s.size();
        vector<bool> dp(n + 1);
        dp[0] = true;
        s = ' ' + s;
        for (int i = 1; i <= n; i++)
        {
            for (int j = i; j >= 1; j--)
            {
                bool ret = false;
                string tmp = s.substr(j, i - j + 1);
                auto pos = find(wordDict.begin(), wordDict.end(), tmp);
                if (pos != wordDict.end())
                {
                    ret = true;
                }
                if (dp[j - 1] && ret)
                {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[n];
    }
};